<?php


namespace backend\controllers;


use common\helpers\commonApi;
use common\helpers\ValidateApi;
use common\models\Campus;
use common\models\System;
use Yii;

/**
 * 校区管理
 * Class CampusController
 * @package backend\controllers
 */
class CampusController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = Campus::className();
    }

    public function actionAdd()
    {
        if (Yii::$app->request->isPost){
            $data = Yii::$app->request->post();
            $model = new $this->model();
            $street = $data['street']??'';
            $data['address'] = $data['address']??'';
            $data['address'] = $street.$data['address'];
            if (!$model->load($data, '')) {
                return commonApi::message('无提交数据', false);
            }
            if (!$model->validate()) {
                return commonApi::message(commonApi::getModelError($model), false);
            }
            if (!ValidateApi::isMobilePhone($model->phone)){
                return commonApi::message('手机号输入错误!',false);
            }
            $result = $model->save(false);
            if ($result) {
                return commonApi::message('添加成功', true);
            } else {
                return commonApi::message( '添加失败', false);
            }
        }
        $map_key = System::getModel()->map_key;
        return $this->render('',['map_key'=>$map_key]);
    }

    public function actionEdit()
    {
        if (Yii::$app->request->isPost){
            $data = Yii::$app->request->post();
            if (!isset($data['id']) || !$data['id']) {
                return commonApi::message('缺少主键参数', false);
            }
            $model = $this->model::findOne($data['id']);
            if (!$model) {
                return commonApi::message('信息不存在', false);
            }
            $street = $data['street']??'';
            $data['address'] = $data['address']??'';
            $data['address'] = $street.$data['address'];
            if (!$model->load($data, '')) {
                return commonApi::message('无提交数据', false);
            }
            if (!$model->validate()) {
                return commonApi::message(commonApi::getModelError($model), false);
            }
            if (!ValidateApi::isMobilePhone($model->phone)){
                return commonApi::message('手机号输入错误!',false);
            }
            $result = $model->save(false);
            if ($result !== false) {
                return commonApi::message('保存成功', true);
            } else {
                return commonApi::message('保存失败', false);
            }
        }
        $info = [];
        $data = Yii::$app->request->get();
        if (isset($data['id']) && $data['id']) {
            $model = $this->model::findOne($data['id']);
            $info = $model->toArray();
        }
        if (empty($info)) {
            return $this->tError('参数或信息错误');
        }
        $map_key = System::getModel()->map_key;
        $info['addr'] = $info['province'].$info['city'].$info['county'].$info['address'];
        return $this->render('', ['info' => $info, 'input' => $data,'map_key'=>$map_key]);
    }

    public function actionSet()
    {
        if (Yii::$app->request->isPost){

        }
        $model = System::getModel();
        return $this->render('',['model'=>$model]);
    }
}